# delimit ;
clear ;
cd "replication" ;

* *************************************************************************** ;
* load data
* *************************************************************************** ;

use "./data/baseline.dta", clear ;

* *************************************************************************** ;
* variables for baseline summary statistics table 
* *************************************************************************** ;

local hhchars "hhsize hhincome incomepc hh_has_tv hh_has_bike hh_has_scooter hh_has_computer hh_has_phone hh_has_nonfarm hh_has_water" ; 
local respchars "female age married hindu completed_elementary completed_secondary mfi inconsistent riskaverse resp_has_income" ;
local health_know "aids_curable nightblindness_curable wash_defecation" ;
local health_behav "water_boil_filter water_tap" ; 

* *************************************************************************** ;
* re-label variables to create LaTeX table
* *************************************************************************** ;

label var hhsize "\textit{Panel A. Household (HH) Variables}\\\\$\qquad$ HH size" ;
label var hhincome "$\qquad$ HH monthly income (Rs.)" ;
label var incomepc "$\qquad$ HH monthly income per capita (Rs.)" ;
label var hh_has_tv "$\qquad$ HH has TV" ;
label var hh_has_bike "$\qquad$ HH has bicycle" ;
label var hh_has_scooter_motor "$\qquad$ HH has scooter/motor" ;
label var hh_has_computer "$\qquad$ HH has computer" ;
label var hh_has_phone "$\qquad$ HH has mobile phone" ;
label var hh_has_nonfarm "$\qquad$ HH has nonfarm enterprise" ; 
label var hh_has_water "$\qquad$ HH has water connection" ; 

label var female "\\\textit{Panel B. Respondent Variables}\\\\$\qquad$ Woman" ;
label var age "$\qquad$ Age" ;
label var married "$\qquad$ Married" ;
label var hindu "$\qquad$ Hindu" ;
label var completed_elementary "$\qquad$ Completed elementary school" ;
label var completed_secondary "$\qquad$ Completed secondary school" ;
label var mfi "$\qquad$ Microfinance client" ; 
label var inconsistent "$\qquad$ Has inconsistent time preferences" ;
label var riskaverse "$\qquad$ Is risk averse" ;
label var resp_has_income "$\qquad$ Has any income during last 12 months" ; 

label var aids_curable "\\\textit{Panel C. Health Variables}\\\\$\qquad$ Knows AIDS is not curable" ;
label var nightblindness_curable "$\qquad$ Knows nightblindness is curable" ;
label var wash_defecation "$\qquad$ Knows should wash hands after defecation" ;
label var water_boil_filter "$\qquad$ Drinks filtered or boiled water" ;
label var water_tap "$\qquad$ Uses a water vessel with tap" ;

****************************************************************************** ;
* build table of summary statistics
****************************************************************************** ;

gen varname = "" ;
gen mean_full = "" ;
gen sd_full = "" ;
gen mean_healthonly = "" ; 
gen sd_healthonly = "" ;
gen mean_healthandpay = "" ;
gen sd_healthandpay = "" ;
gen mean_control = "" ;
gen sd_control = "" ;
gen pval = "" ; 
gen stars = "" ;

label var varname "" ;
label var mean_full "Mean" ;
label var mean_healthonly "\shortstack[c]{Health\\Only}" ;
label var mean_healthandpay "\shortstack[c]{Health\\+Pay}" ;
label var mean_control "Control" ;
label var sd_full "SD" ;
label var sd_healthonly "\shortstack[c]{Standard  \\ Deviation}" ;
label var sd_healthandpay "\shortstack[c]{Standard  \\ Deviation}" ;
label var sd_control "\shortstack[c]{Standard  \\ Deviation}" ;
label var pval "\shortstack[c]{\(F\)-test \\ \(p\)-value}" ; 

****************************************************************************** ;
* generate randomization strata and wave-class variable
****************************************************************************** ;

egen strata = group(wave neighborhood female mfi) ;
egen waveclass = group(wave class) ;

****************************************************************************** ;
* get sample sizes, to put in the summary stats table header
****************************************************************************** ;

qui summ ; 
local N = r(N) ; 

count if healthonly == 1 ;
local Nhealth = r(N) ;

count if healthandpay == 1 ;
local Nhealthandpay = r(N) ;

count if control == 1 ;
local Ncontrol = r(N) ;

label var mean_healthonly "\shortstack[c]{HEE \\ (\$ N=`Nhealth'$) }" ;
label var mean_healthandpay "\shortstack[c]{HEEC\\ (\$ N=`Nhealthandpay'$)}" ;
label var mean_control "\shortstack[c]{Control\\ (\$ N=`Ncontrol'$)}" ;

****************************************************************************** ;
* get summary statistics
****************************************************************************** ;

local i = 1 ;

foreach var of varlist `hhchars' `respchars' `health_know' `health_behav' { ;
	di "`var'" ;
	local vartext: variable label `var' ;
	replace varname = "`vartext'" in `i' ; 
	
	qui summ `var' ;
	qui replace mean_full = string(r(mean), "%9.2f") in `i' ;
	qui replace sd_full = string(r(sd), "%9.2f") in `i' ;
	
	qui summ `var' if healthonly == 1 ;
	qui replace mean_healthonly = string(r(mean), "%9.2f") in `i' ;
	qui replace sd_healthonly = string(r(sd), "%9.2f") in `i' ;

	qui summ `var' if healthandpay == 1 ;
	qui replace mean_healthandpay = string(r(mean), "%9.2f") in `i' ;
	qui replace sd_healthandpay = string(r(sd), "%9.2f") in `i' ;

	qui summ `var' if control == 1 ;
	qui replace mean_control = string(r(mean), "%9.2f") in `i' ;
	qui replace sd_control = string(r(sd), "%9.2f") in `i' ;
	
	areg `var' healthonly healthandpay , a(strata) robust ;
	test healthonly healthandpay ;
	qui replace pval = string(r(p), "%9.3f") in `i' ;
	qui replace pval = "" if pval == "." ;
	local ++i ;

} ;

* calculate coefficient of variation ;
gen cv = "" ;
label var cv "CV" ; 
replace cv = string(real(sd_full)/real(mean_full), "%9.2f") if strpos(var, "HH size") > 0 ;
replace cv = string(real(sd_full)/real(mean_full), "%9.2f") if strpos(var, "HH monthly income") > 0 ;
replace cv = string(real(sd_full)/real(mean_full), "%9.2f") if strpos(var, "Age") > 0 ;

keep varname mean* sd_full cv pval stars  ;
order varname mean_full sd_full cv ;
dropmiss, obs force ;

replace stars = "*" if real(pval) < 0.1 ;
replace stars = "**" if real(pval) < 0.05 ;
replace stars = "***" if real(pval) < 0.01 ;

****************************************************************************** ;
* output table 
****************************************************************************** ;

texsave using "./output/table-baseline-summstats.tex",
	replace frag nofix
	title("Baseline Summary Statistics") 
	marker(table-baseline-summstats)
	varlabels
	align(lrrrrrrr@{}l)
	width(\linewidth)
	footnote("\vspace{-3ex} \textit{Notes:} 
		Data from baseline survey. 
		In Column 3, the Coefficient of Variation (CV) is the ratio of the standard deviation to the mean and 
		is calculated for continuous variables.
		The last column reports the \(p\)-value of the joint hypothesis test \(H_0: \beta_1 = 0, \beta_2 = 0\), 
		where the regression is the same specification used for the impact analysis (Equation \ref{reg-equation}) and the 
		dependent variable is a baseline outcome. The \(p\)-values are not reported for two baseline outcomes---i.e., the dummy for woman and the dummy for whether the respondent is a microfinance client---because they are contained in the strata fixed effects in the regression.		
		***\$\,p < 0.01$, **\$\,p < 0.05$, *\$\,p<0.10$.", width(p{\linewidth})) ;

filefilter "./output/table-baseline-summstats.tex" "./output/table-baseline-summstats-v00.tex",
	from("\BSmulticolumn{9}{p{\BSlinewidth}}") to("\BSmulticolumn{9}{p{0.99\BSlinewidth}}") replace ;	
filefilter "./output/table-baseline-summstats-v00.tex" "./output/table-baseline-summstats-v01.tex",
	from("\BSbottomrule \BSaddlinespace\BS[1.5ex\BS]") to("\BSbottomrule \BSaddlinespace\BS[0.5ex\BS] ") replace ;
filefilter "./output/table-baseline-summstats-v01.tex" "./output/table-baseline-summstats-v02.tex",
	from("\BSbegin{footnotesize}") to("\BSfootnotesize ") replace ;
filefilter "./output/table-baseline-summstats-v02.tex" "./output/table-baseline-summstats-v03.tex",
	from("\BSend{footnotesize}") to("") replace ;
filefilter "./output/table-baseline-summstats-v03.tex" "./output/table-baseline-summstats-v04.tex",
	from("\BSend{footnotesize}") to("") replace ;
filefilter "./output/table-baseline-summstats-v04.tex" "./output/table-baseline-summstats.tex",
	from("\BStoprule") 
	to("\BStoprule & \BSmulticolumn{3}{c}{\BSshortstack[c]{Full Sample (\$N = `N'$)}} & \BSmulticolumn{3}{c}{\BSshortstack[c]{Means by Study Arm}} & \BStabularnewline \BScmidrule(lr){2-4} \BScmidrule(lr){5-7}") 
	replace ;
	
forvalues i = 0/4 {;
	erase "./output/table-baseline-summstats-v0`i'.tex" ;
} ;

exit ;


